package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManagerService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ author     ：廖传港
 * @ date       ：Created in 2020/12/21 19:48
 * @ description：实现类
 * @ modified By：
 * @ ersion:
 */
@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;
    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;
    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;


    @Override
    public List<BaseCategory1> getCategory1() {

        return baseCategory1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
        return baseCategory2Mapper.selectList(new QueryWrapper<BaseCategory2>().eq("category1_id",category1Id));
    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        return baseCategory3Mapper.selectList(new QueryWrapper<BaseCategory3>().eq("category2_id",category2Id));
    }

    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> selectAttrInfoList=baseAttrInfoMapper.selectAttrInfoList(category1Id, category2Id, category3Id);

        return selectAttrInfoList;
    }



//    添加/修改平台属性
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {

        if(baseAttrInfo.getId().equals("")&& baseAttrInfo.getId().equals(null)){
            //1:平台属性表
            baseAttrInfoMapper.insert(baseAttrInfo);
            //2平台属性值表 lombda
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            attrValueList.forEach((attrValue) -> {
                //外键
                attrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(attrValue);
            });
        }else {
            //1:平台属性表
            baseAttrInfoMapper.updateById(baseAttrInfo);
            //2平台属性值表 lombda
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            attrValueList.forEach((attrValue) -> {
                //外键
                attrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.updateById(attrValue);
            });
        }

    }


    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Override
    public BaseAttrInfo getAttrValueList(Long attrId) {
        List<BaseAttrValue> BaseAttrValue = baseAttrValueMapper.selectList(new QueryWrapper<BaseAttrValue>().eq("attr_id", attrId));
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        baseAttrInfo.setAttrValueList(BaseAttrValue);
        return baseAttrInfo;
    }

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    //查询品牌分页列表
    @Override
    public IPage<BaseTrademark> baseTrademark(Integer page, Integer limit) {

        //select * from base_trademark where .. limit page,limit
        return baseTrademarkMapper.selectPage(new Page<BaseTrademark>(page,limit),
                null);
    }

    @Override
    public BaseTrademark getbaseTrademark(Integer id) {
        return baseTrademarkMapper.selectById(id);
    }

    @Override
    public void update(BaseTrademark trademark) {
        baseTrademarkMapper.updateById(trademark);
    }

    @Override
    public void save(BaseTrademark trademark) {
        baseTrademarkMapper.insert(trademark);
    }

    @Override
    public void removeBaseTrademark(Long id) {
        baseTrademarkMapper.deleteById(id);
    }

    @Autowired
    private SpuInfoMapper spuInfoMapper;
    @Override
    public IPage<SpuInfo> spuList(Integer page, Integer limit, Long category3Id) {
        return spuInfoMapper.selectPage(new Page<SpuInfo>(page, limit), new QueryWrapper<SpuInfo>().eq("category3_id", category3Id));
    }

    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;

    @Override
    public List<BaseSaleAttr> baseSaleAttrList() {
        return baseSaleAttrMapper.selectList(null);
    }


    @Override
    public List<BaseTrademark> getTrademarkList() {
        return baseTrademarkMapper.selectList(null);
    }


    @Autowired
    private SpuImageMapper spuImageMapper;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;


    //保存spu
    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1: spu_info  1
        spuInfoMapper.insert(spuInfo);
        //2:spu_image  N
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        spuImageList.forEach(spuImage -> {
            //外键
            spuImage.setSpuId(spuInfo.getId());
            spuImageMapper.insert(spuImage);
        });
        //3: spu_sale_attr N
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        spuSaleAttrList.forEach(spuSaleAttr -> {
            //外键
            spuSaleAttr.setSpuId(spuInfo.getId());
            spuSaleAttrMapper.insert(spuSaleAttr);
            //4: spu_sale_value N中N
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            spuSaleAttrValueList.forEach(spuSaleAttrValue -> {
                //外键
                spuSaleAttrValue.setSpuId(spuInfo.getId());
                //销售属性的名称
                spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                spuSaleAttrValueMapper.insert(spuSaleAttrValue);
            });
        });
    }

    @Override
    public List<SpuImage> spuImageList(Long spuId) {

        return spuImageMapper.selectList(new QueryWrapper<SpuImage>().eq("spu_id", spuId));
    }

    @Override
    public List<SpuSaleAttr> spuSaleAttrList(Long spuId) {

        return spuSaleAttrMapper.spuSaleAttrList(spuId);
    }

    @Autowired
    private SkuImageMapper skuImageMapper;
    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;
    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;

    //保存sku
    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        //1.先保存外层
        skuInfoMapper.insert(skuInfo);
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        //2.保存skuImageList
        skuImageList.forEach(skuImage -> {
            skuImage.setSkuId(skuInfo.getSpuId());
            skuImageMapper.insert(skuImage);
        });
        //3.保存SkuAttrValue
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        skuAttrValueList.forEach(skuAttrValue -> {
            skuAttrValue.setSkuId(skuInfo.getSpuId());
            skuAttrValueMapper.insert(skuAttrValue);
        });
        //3.保存SkuSaleAttrValue
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        skuSaleAttrValueList.forEach(skuSaleAttrValue -> {
            // 库存表的外键
            skuSaleAttrValue.setSkuId(skuInfo.getId());
            //商品表的外键
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
            skuSaleAttrValueMapper.insert(skuSaleAttrValue);
        });

    }

    @Autowired
    private SkuInfoMapper skuInfoMapper;
    @Override
    public IPage<SkuInfo> list(Integer page, Integer limit) {

        return skuInfoMapper.selectPage(new Page<SkuInfo>(page,limit),null);
    }

    //上架
    @Override
    public void onSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.ISSALE);
        skuInfoMapper.updateById(skuInfo);
        //TODO 发消息 给MQ 通知其它微服务 ES
    }


    //下架
    @Override
    public void cancelSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.CANCELSALE);
        skuInfoMapper.updateById(skuInfo);
        //TODO 发消息 给MQ 通知其它微服务 ES
    }



}

